using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Web;

using LocalBoard.Domain;
using LocalBoard.Controller;

namespace LocalBoard.Web
{
	/// <summary>
	/// Summary description for ClubPage.
	/// </summary>
	public class ClubPage : ClubSecurePage
	{
		private Club _club = null;
		public Club Club
		{
			get {return _club;}
		}
		private ClubController _clubController = null;
		public ClubController ClubController
		{
			get {return _clubController;}
		}
		public virtual BoardController BoardController
		{
			get
			{
				if(ClubController != null)
					return new ClubBoardController(ClubController, GetCurrentPageNo(), Global.AccessorFactory);
				else
					return null;
			}
		}
		public Profile ClubUser
		{
			get
			{
				if(User == null)
//				{
//					if(AllowAnonymousUser)
						return Security.AnonymousUser;
//					else
//						throw new ApplicationException("User is not authenticated and current configuration does not allow Anonymous user.");
//				}
				else
					return User.UserProfile;
			}
		}

		public int GetCurrentPageNo()
		{
			try
			{
				return Convert.ToInt32(Request.QueryString["page"]);
			}
			catch
			{
				return 0;
			}
		}

		#region Public static method
		public static string AddPageQueryStringToUrl(string url, int pageNo)
		{
			return AddPageQueryStringToUrl(url, pageNo.ToString());
		}
		public static string AddPageQueryStringToUrl(string url, string pageNo)
		{
			return AddQueryStringToUrl(url, "page", pageNo);
		}

		public static string AddMessageQueryStringToUrl(string url, int messageId)
		{
			return AddMessageQueryStringToUrl(url, messageId.ToString());
		}
		public static string AddMessageQueryStringToUrl(string url, string messageId)
		{
			return AddQueryStringToUrl(url, "message", messageId);
		}

		public static string AddBoardQueryStringToUrl(string url, int boardId)
		{
			return AddBoardQueryStringToUrl(url, boardId.ToString());
		}
		public static string AddBoardQueryStringToUrl(string url, string boardId)
		{
			return AddQueryStringToUrl(url, "board", boardId);
		}

		public static string AddClubQueryStringToUrl(string url, int clubId)
		{
			return AddClubQueryStringToUrl(url, clubId.ToString());
		}
		public static string AddClubQueryStringToUrl(string url, string clubId)
		{
			return AddQueryStringToUrl(url, "club", clubId);
		}

		public static string AddClubBoardMessageQueryStringToUrl(string url, string clubId, string boardId, string messageId)
		{
			return AddMessageQueryStringToUrl(AddBoardQueryStringToUrl(AddClubQueryStringToUrl(url, clubId), boardId), messageId);
		}

		public static string AddUserQueryStringToUrl(string url, string userId)
		{
			return AddQueryStringToUrl(url, "userid", userId);
		}

		public static string AddQueryStringToUrl(string url, string queryKey, string queryValue)
		{
			string[] splitStr = url.Split('?');

			return splitStr[0] + "?" + (splitStr.Length > 1 ? ConstructQueryString(GetQueryKeyValuePairs(splitStr[1], queryKey, queryValue)) : queryKey + "=" + queryValue);
		}

		private static Hashtable GetQueryKeyValuePairs(string queryPart, string queryKey, string queryValue)
		{
			Hashtable queryKeyValuePairs = new Hashtable();
			foreach(string str in queryPart.Split('&'))
			{
				string[] querySplitStr = str.Split('=');
				queryKeyValuePairs.Add(querySplitStr[0], querySplitStr.Length > 1 ? querySplitStr[1] : "");
			}
			if(queryKeyValuePairs.ContainsKey(queryKey))
				queryKeyValuePairs[queryKey] = queryValue;
			else
				queryKeyValuePairs.Add(queryKey, queryValue);
			return queryKeyValuePairs;
		}

		private static string ConstructQueryString(Hashtable queryKeyValuePairs)
		{
			StringBuilder queryBuilder = new StringBuilder();
			int index = 0;
			foreach(string key in queryKeyValuePairs.Keys)
			{
				if(index > 0)
					queryBuilder.Append("&");
				queryBuilder.Append(key + (queryKeyValuePairs[key].ToString().Length > 0 ? "=" + queryKeyValuePairs[key].ToString() : ""));
				index++;
			}
			return queryBuilder.ToString();
		}
		#endregion

		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			GetCurrentClub();
			if(_club != null)
				_clubController = new ClubController(_club.Id, Global.AccessorFactory);
			base.OnInit(e);
		}

		#region Private helper
		private void GetCurrentClub()
		{
			int clubId = 0;
			string clubIdString = GetClubIdString();
			if(clubIdString != null && clubIdString.Length > 0)
			{
				try
				{
					clubId = Convert.ToInt32(clubIdString);
				}
				catch
				{}
				AdminController adminController = new AdminController(Global.AccessorFactory);
				if(clubId > 0)
					_club = adminController.GetClub(clubId);
				else
					_club = adminController.GetClub(clubIdString);
			}
		}

		protected virtual string GetClubIdString()
		{
			return HttpContext.Current.Request.QueryString["club"];
		}
		#endregion
	}
}
